<template>
    <div class="edu-page-warp">
        <ConditionCard
            shadow="never"
            body-style="padding: 24px 24px 6px 24px;"
            @search="filterSubmit"
            @reset="reset('queryForm')"
        >
             <el-form ref="queryForm" :model="queryForm" label-position="right" label-width="100px">
                <el-row :gutter="24" style="height: 50px;">
                    <!-- <el-col :span="8">
                        <el-form-item label="管理部门:" prop="projId">
                            <el-select
                                v-model="queryForm.projId"
                                @change="changeProj"
                                style="width: 100%"
                            >
                                <el-option
                                    v-for="item in options.project"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>-->
                    <el-col :span="8">
                        <el-form-item label="年级:" prop="grade"
                        :error="(queryForm.projId=='1' && !queryForm.grade && !!queryForm.departmentId) ? $t('culture.rules.plsChooseGrade'):''"
                        >
                            <el-date-picker
                                v-model="queryForm.grade"
                                @change="(queryForm.projId=='1') ? queryCampusMajor() : ''"
                                value-format="yyyy"
                                type="year"
                                placeholder="选择学年"
                                style="width: 100%"
                            />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="入学季节:" prop="enrolSeason">
                            <el-select
                                v-model="queryForm.enrolSeason"
                                style="width: 100%"
                                clearable
                            >
                                <el-option
                                    v-for="item in options.enrolSeason"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="培养层次:" prop="trainingLevel">
                            <el-select
                                v-model="queryForm.trainingLevel"
                                style="width: 100%"
                                clearable
                            >
                                <el-option
                                    v-for="item in options.trainingLevel"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8" v-if="queryForm.projId == '1'" class="ie-hack">
                        <el-form-item label="类别:" prop="professionCateGory">
                            <el-select
                                v-model="queryForm.professionCateGory"
                                style="width: 100%;"
                                clearable
                            >
                                <el-option
                                    v-for="item in options.professionCateGory"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="校区:" prop="campus">
                            <el-select v-model="queryForm.campus" clearable style="width: 100%">
                                <el-option
                                    v-for="item in options.campus"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8" style="height:51px">
                        <el-form-item label="学院:" prop="departmentId">
                            <el-select
                                v-model="queryForm.departmentId"
                                filterable
                                style="width: 100%"
                                @change="queryCampusMajor"
                                clearable
                            >
                                <el-option
                                    v-for="item in options.faculty"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <template v-if="queryForm.projId == '1'">
                        <el-col :span="8">
                            <el-form-item filterable label="校内专业:" prop="campusProfession">
                                <el-select
                                    v-model="queryForm.campusProfession"
                                    style="width: 100%"
                                    clearable
                                >
                                    <el-option
                                        v-for="item in options.campusProfessions"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value"
                                    />
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </template>
                    <template v-else>
                        <el-col :span="8">
                            <el-form-item filterable label="专业:" prop="major">
                                <el-select v-model="queryForm.major" style="width: 100%" clearable>
                                    <el-option
                                        v-for="item in options.major"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value"
                                    />
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="学习形式:" prop="formLearning">
                                <el-select
                                    v-model="queryForm.formLearning"
                                    style="width: 100%"
                                    clearable
                                >
                                    <el-option
                                        v-for="item in options.formLearning"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value"
                                    />
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="培养类别:" prop="trainingCategory">
                                <el-select
                                    v-model="queryForm.trainingCategory"
                                    style="width: 100%"
                                    clearable
                                >
                                    <el-option
                                        v-for="item in options.trainingCategory"
                                        :key="item.code"
                                        :label="item.name"
                                        :value="item.code"
                                    />
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item
                                :label="$t('culturePlan.degreeType') + ':'"
                                prop="degreeType"
                            >
                                <el-select
                                    v-model="queryForm.degreeType"
                                    @change="queryCampusMajor"
                                    style="width: 100%;"
                                    clearable
                                >
                                    <el-option
                                        v-for="item in options.degreeType"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value"
                                    />
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </template>
                    <el-col :span="8" class="ie-hack">
                        <el-form-item label="在校状态:" prop="leaveSchool">
                            <el-select
                                v-model="queryForm.leaveSchool"
                                filterable
                                style="width: 100%"
                                clearable
                            >
                                <el-option
                                    v-for="item in options.leaveSchool"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="是否国际学生:" prop="isOverseas">
                            <el-select
                                v-model="queryForm.isOverseas"
                                filterable
                                style="width: 100%"
                                clearable
                            >
                                <el-option
                                    v-for="item in options.isOverseas"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="关联状态:" prop="associationStatus">
                            <el-select
                                v-model="queryForm.associationStatus"
                                style="width: 100%;"
                                clearable
                            >
                                <el-option label="未关联" value="2"/>
                                <el-option label="已关联" value="1"/>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="专项计划:" prop="spcialPlan">
                            <el-select
                                v-model="queryForm.spcialPlan"
                                placeholder="请选择"
                                style="width: 100%;"
                            >
                                <el-option
                                    v-for="item in options.spcialPlan"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="导师:" prop="associationStatus">
                            <el-input
                                v-model="queryForm.teacherName"
                                placeholder="输入姓名/工号"
                            />
                        </el-form-item>
                    </el-col> 
                </el-row>
            </el-form>
        </ConditionCard>
        <el-card v-loading="loading" shadow="never" body-style="padding: 24px;" class="card-margin">
            <h1 class="condition-title">{{ $t('culture.cultureScheme.linkPlan') }}</h1>
            <div class="div-main clearfix">
                <div class="float-left">
                    <el-input
                        v-model="queryForm.name"
                        placeholder="输入姓名/学号搜索"
                        prefix-icon="el-icon-search"
                        clearable
                        @change="handSearchChang"
                        @keyup.enter.native="handSearchChang"
                    />
                </div>
                <div class="float-right">
                    <el-button type="primary" @click="pageExport"
                        >页面导出</el-button
                    >
                    <el-button @click="batchAddRel">关联方案</el-button>
                    <el-button @click="batchCancelRel">取消关联</el-button>
                    <el-button type="primary" @click="autoatchAddRel">自动关联</el-button>
                </div>
            </div>
            <!-- <el-table ref="studentTable" 
                :data="tableData" 
                stripe 
                style="width: 100%" 
                :cell-class-name="cellStyleMethod"  
                tooltip-effect="light"
            >
                <el-table-column type="expand">
                    <template slot-scope="props">
                        <div class="edu-auto-row">
                            <div class="edu-auto-col col">
                                <div class="edu-auto-label">学号：</div>
                                <div class="edu-auto-content">{{ props.row.studentId }}</div>
                            </div>
                            <div class="edu-auto-col col">
                                <div class="edu-auto-label">姓名：</div>
                                <div class="edu-auto-content">{{ props.row.name }}</div>
                            </div>
                            <div class="edu-auto-col col">
                                <div class="edu-auto-label">年级：</div>
                                <div class="edu-auto-content">{{ props.row.grade }}</div>
                            </div>
                            <div class="edu-auto-col col">
                                <div class="edu-auto-label">学院：</div>
                                <div class="edu-auto-content">{{ props.row.departmentId }}</div>
                            </div>
                            <div class="edu-auto-col col">
                                <div class="edu-auto-label">专业：</div>
                                <div class="edu-auto-content">{{ props.row.major }}</div>
                            </div>
                            <div class="edu-auto-col col">
                                <div class="edu-auto-label">培养类别：</div>
                                <div class="edu-auto-content">{{ props.row.trainingCategory }}</div>
                            </div>
                            <div class="edu-auto-col col">
                                <div class="edu-auto-label">学位类型：</div>
                                <div class="edu-auto-content">{{ props.row.degreeType }}</div>
                            </div>
                            <div class="edu-auto-col col">
                                <div class="edu-auto-label">学习形式：</div>
                                <div class="edu-auto-content">{{ props.row.formLearning }}</div>
                            </div>
                            <div class="edu-auto-col col">
                                <div class="edu-auto-label">专项计划：</div>
                                <div class="edu-auto-content">{{ props.row.spcialPlan }}</div>
                            </div>
                            <div class="edu-auto-col col">
                                <div class="edu-auto-label">关联状态：</div>
                                <div class="edu-auto-content">
                                    <el-tag
                                        v-if="props.row.associationStatus == 1"
                                        type="success"
                                    >已关联</el-tag>
                                    <el-tag
                                        v-if="props.row.associationStatus == 2"
                                        type="danger"
                                    >未关联</el-tag>
                                </div>
                            </div>
                            <div class="edu-auto-col col" style="width:45%">
                                <div class="edu-auto-label">培养方案:</div>
                                <div class="edu-auto-content">{{ props.row.cultureName }}</div>
                            </div>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column type="selection" width="45"/>
                <el-table-column label="学号" prop="studentId"/>
                <el-table-column label="姓名" prop="name"/>
                <el-table-column label="年级" prop="grade" width="70"/>
                <el-table-column label="培养层次" prop="trainingLevel" width="80"/>
                <el-table-column label="学院" prop="departmentId" min-width="150px" show-overflow-tooltip/>
                <el-table-column label="专业" prop="major" min-width="150px" show-overflow-tooltip/>
                <el-table-column label="培养类别" prop="trainingCategory" width="90"/>
                <el-table-column label="学位类型" prop="degreeType" width="80"/>
                <el-table-column label="专项计划" prop="spcialPlan" width="160"/>
                <el-table-column :label="$t('dic.formLearning')" prop="formLearning" width="80"/>
                <el-table-column label="培养方案" min-width="260px" show-overflow-tooltip>
                    <template slot-scope="scope">
                         <a
                            v-if="scope.row.cultureId"
                            type="text"
                            @click="preview(scope.row)"
                        >{{ scope.row.cultureName }}
                        </a>
                    </template>
                </el-table-column>
                <el-table-column label="导师" prop="teacherName" width="80"/>
                <el-table-column label="关联状态" width="100">
                    <template slot-scope="scope">
                        <el-tag
                            v-if="scope.row.associationStatus == 2"
                            type="danger"
                            size="mini"
                            disable-transitions
                        >未关联</el-tag>
                        <el-tag
                            v-if="scope.row.associationStatus == 1"
                            type="success"
                            size="mini"
                            disable-transitions
                        >已关联</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="80px">
                    <template slot-scope="scope">
                        <el-button
                            v-if="scope.row.associationStatus == 2"
                            type="text"
                            @click="addStudentCultureRelByManual(scope.row)"
                        >关联方案</el-button>
                        <el-button
                            v-if="scope.row.associationStatus == 1"
                            type="text"
                            @click="cancelStudentCultureRelByManual(scope.row)"
                        >取消关联</el-button>
                    </template>
                </el-table-column>
            </el-table> -->
            <el-table ref="studentTable" 
                :data="tableData" 
                stripe 
                style="width: 100%" 
                :cell-class-name="cellStyleMethod"  
                tooltip-effect="light"
            >
                <el-table-column type="expand">
                    <template slot-scope="props">
                        <div class="edu-auto-row">
                            <div class="edu-auto-col col">
                                <div class="edu-auto-label">学号：</div>
                                <div class="edu-auto-content">{{ props.row.studentId }}</div>
                            </div>
                            <div class="edu-auto-col col">
                                <div class="edu-auto-label">姓名：</div>
                                <div class="edu-auto-content">{{ props.row.name }}</div>
                            </div>
                            <div class="edu-auto-col col">
                                <div class="edu-auto-label">年级：</div>
                                <div class="edu-auto-content">{{ props.row.grade }}</div>
                            </div>
                            <div class="edu-auto-col col">
                                <div class="edu-auto-label">学院：</div>
                                <div class="edu-auto-content">{{ props.row.departmentId }}</div>
                            </div>
                            <div class="edu-auto-col col">
                                <div class="edu-auto-label">专业：</div>
                                <div class="edu-auto-content">{{ props.row.major }}</div>
                            </div>
                            <div class="edu-auto-col col">
                                <div class="edu-auto-label">培养类别：</div>
                                <div class="edu-auto-content">{{ props.row.trainingCategory }}</div>
                            </div>
                            <div class="edu-auto-col col">
                                <div class="edu-auto-label">学位类型：</div>
                                <div class="edu-auto-content">{{ props.row.degreeType }}</div>
                            </div>
                            <div class="edu-auto-col col">
                                <div class="edu-auto-label">学习形式：</div>
                                <div class="edu-auto-content">{{ props.row.formLearning }}</div>
                            </div>
                            <div class="edu-auto-col col">
                                <div class="edu-auto-label">专项计划：</div>
                                <div class="edu-auto-content">{{ props.row.spcialPlan }}</div>
                            </div>
                            <div class="edu-auto-col col">
                                <div class="edu-auto-label">关联状态：</div>
                                <div class="edu-auto-content">
                                    <el-tag
                                        v-if="props.row.associationStatus == 1"
                                        type="success"
                                    >已关联</el-tag>
                                    <el-tag
                                        v-if="props.row.associationStatus == 2"
                                        type="danger"
                                    >未关联</el-tag>
                                </div>
                            </div>
                            <div class="edu-auto-col col" style="width:45%">
                                <div class="edu-auto-label">培养方案:</div>
                                <div class="edu-auto-content">{{ props.row.cultureName }}</div>
                            </div>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column type="selection" width="45"/>
                <el-table-column label="学号" prop="studentId"/>
                <el-table-column label="姓名" prop="name"/>
                <el-table-column label="年级" prop="grade" width="70"/>
                <el-table-column label="培养层次" prop="trainingLevel" width="80"/>
                <el-table-column label="学院" prop="departmentId" min-width="150px" show-overflow-tooltip/>
                <el-table-column label="专业" prop="major" min-width="150px" show-overflow-tooltip/>
                <el-table-column label="培养类别" prop="trainingCategory" width="90"/>
                <el-table-column label="学位类型" prop="degreeType" width="80"/>
                <el-table-column label="专项计划" prop="spcialPlan" width="160"/>
                <el-table-column :label="$t('dic.formLearning')" prop="formLearning" width="80"/>
                <el-table-column label="培养方案" min-width="260px" show-overflow-tooltip>
                    <template slot-scope="scope">
                         <a
                            v-if="scope.row.cultureId"
                            type="text"
                            @click="preview(scope.row)"
                        >{{ scope.row.cultureName }}
                        </a>
                    </template>
                </el-table-column>
                <el-table-column label="导师" prop="teacherName" width="80"/>
                <el-table-column label="关联状态" width="100">
                    <template slot-scope="scope">
                        <el-tag
                            v-if="scope.row.associationStatus == 2"
                            type="danger"
                            size="mini"
                            disable-transitions
                        >未关联</el-tag>
                        <el-tag
                            v-if="scope.row.associationStatus == 1"
                            type="success"
                            size="mini"
                            disable-transitions
                        >已关联</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="80px">
                    <template slot-scope="scope">
                        <el-button
                            v-if="scope.row.associationStatus == 2"
                            type="text"
                            @click="addStudentCultureRelByManual(scope.row)"
                        >关联方案</el-button>
                        <el-button
                            v-if="scope.row.associationStatus == 1"
                            type="text"
                            @click="cancelStudentCultureRelByManual(scope.row)"
                        >取消关联</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="edu-pagination">
                <div class="float-right">
                    <el-pagination
                        :current-page="queryForm.pageNum_"
                        :page-sizes="[20,50,100,500,1000]"
                        :page-size="queryForm.pageSize_"
                        :total="queryForm.total"
                        layout="total, sizes, prev, pager, next, jumper"
                        style="float: right;margin:0;"
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                    />
                </div>
            </div>
        </el-card>
        <el-dialog
            :visible.sync="dialogVisible"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            :before-close="cancel"
            title="关联培养方案"
            width="60%"
        >
            <selectCultureScheme
                ref="modal"
                :current-selected="currentSelected"
                :projId="queryForm.projId"
                :is-show="isShow"
                :dialogStatus="dialogVisible"
                :department-id="departmentId"
                :forms="queryForm"
                @schemeId="getSchemeId"
            />
            <span slot="footer" class="dialog-footer">
                <el-button @click=" cancel">取 消</el-button>
                <el-button type="primary" @click="handleSubmit">确 定</el-button>
            </span>
        </el-dialog>
         <el-dialog
            :visible.sync="dialogOk"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            title="提示"
            width="30%"
            type="warning"
        >
            <div class="waining-color">
                <i class="el-icon-warning"></i>
                <span>自动关联</span>
            </div>
            <p>本次关联培养方案{{tipsData.totalNum}}个，关联成功{{tipsData.bindNum}}个，关联失败{{tipsData.unboundNum}}个</p>
            <a href="#" @click="downloadfile">点击导出excel查看详情</a>
            <span slot="footer" class="dialog-footer">
                <el-button @click="cancelOk">取 消</el-button>
                <el-button type="primary" @click="handleRouter">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
import bus from "common/src/utils/bus";
import { findProfessionByParam } from "common/src/api/culture/cultruescheme";
import { getPower ,exportPage} from 'common/src/api/culture/culturePlan';
import { queryDic, findTrainingLevelByProjId, sortTrainingCategories} from "common/src/api/dictionary";
import { findFaculty } from "common/src/api/faculty";
import * as API from "common/src/api/culture/culturebinding";
import { downloadfile } from 'common/src/api/upload';
import UTILS from "common/src/utils/utils";
import selectCultureScheme from "./selectCultureScheme";
import { getGraduateDegreePage } from "common/src/api/resource/course";
export default {
    name: "CultureBinding",
    components: {
        selectCultureScheme
    },
    data() {
        return {
            loading: false,
            dialogVisible: false,
            dialogOk: false,
            popoverVisible: false,
            isShow: false,
            queryForm: {
                grade: "", // 年级
                enrolSeason: "", // 入学季节
                studentId: "", // 学号
                trainingLevel: "", // 培养层次
                trainingCategory: '', // 培养类别
                campus: "", // 校区
                leaveSchool: "1", // 在校状态
                isOverseas: "", // 是否国际学生
                departmentId: "", // 学院
                major: "", // 专业
                degreeType: "", // 学位类型
                studentCategory: "", // 学位类别
                formLearning: "", // 学习形式
                spcialPlan: "", // 专项计划
                studentAbroad: "", // 国际学生
                associationStatus: "", // 状态
                name: "", // 名称
                pageNum_: 1,
                pageSize_: 20,
                total: 0,
                projId: "1",
                campusProfession: "",
                teacherName:""
            },
            // 下拉相关选项数据
            options: {
                grade: [],
                enrolSeason: [],
                trainingLevel: [],
                campus: [],
                faculty: [],
                major: [],
                degreeType: [], // 学位类型
                spcialPlan: [],
                formLearning: [],
                leaveSchool: [],
                isOverseas: [],
                studentCategory: [], // 学位类别
                trainingCategory: [], // 培养类别
                studentAbroad: [
                    {
                        label: "全部",
                        value: ""
                    },
                    {
                        label: "全部",
                        value: ""
                    },
                    {
                        label: "全部",
                        value: ""
                    }
                ],
                status: [],
                templateName: [],
                project: [],
                professionCateGory: [
                    { value: "1", label: "专业大类" },
                    { value: "2", label: "专业" }
                ],
                campusProfessions: []
            },
            tableData: [],
            currentSelected: {},
            tipsData:'',
            schemeId: "",
            role:'',
            departmentId:''
        };
    },
    created() {
        this.init();
        this.initProjId();
        this.changeProj(this.queryForm.projId);
        bus.$on("updateCurrentManageDeptId", id => {
            this.queryForm.projId = id;
            this.changeProj(id);
            this.handleSearch();
        });
    },
    watch: {
        dialogVisible(newVal, oldVal) {
            this.isShow = newVal;
        },
    },
    methods: {
        cellStyleMethod({row, column, rowIndex, columnIndex}){
            if (columnIndex == 12) {
                return 'normal-cell';
            }
        },
        // 预览模板
        preview(row) {
            let schemeDetails =  {
                    projId: this.queryForm.projId,
                    id: row.cultureId,
                    dataType: 2,
                    nameCN:row.cultureName,
                    nameEN:row.cultureNameEn
                }
            sessionStorage.setItem("schemeDetails",JSON.stringify(schemeDetails));
            this.$router.push({
                name: "cultureSchemePreview",
                params: {
                    projId: this.queryForm.projId,
                    id: row.cultureId,
                    dataType: 2
                }
            });
        },
        cancel() {
            this.dialogVisible = false;
            this.dialogOk = false;
            this.$refs.modal.dataInit();
        },
        cancelOk() {
            this.dialogOk = false;
            this.handleSearch();
        },
        initProjId() {
            let currentUser =
                (this.$store.state.session && this.$store.state.session.user) ||
                JSON.parse(localStorage.getItem("user"));
            this.queryForm.projId = currentUser.currentManageDptId;
        },
        changeProj(val) {
            this.queryTrainingLevel(val);
            this.handleSearch();
        },
        queryCampusMajor() {
            this.queryForm.major = "";
            this.queryForm.campusProfession = "";
            if (this.queryForm.projId == "1") {
                this.queryCampusProfessions();
            } else this.queryMajors();
        },
        queryCampusProfessions() {
            this.options.campusProfessions = [];
            // 联动校验
            if(!(this.queryForm.grade && this.queryForm.departmentId)) {
              return
            }
            const query = {};
            query.category = this.queryForm.professionCateGory;
            query.faculty = this.queryForm.departmentId;
            query.grade = this.queryForm.grade;
            query.trainingLevel = this.queryForm.trainingLevel;
            findProfessionByParam(query).then(res => {
                if (res.code == 200) {
                    this.options.campusProfessions = res.data.map(
                        (item, index) => {
                            return {
                                label: item.professionCateGoryName,
                                value: item.professionCateGory
                            };
                        }
                    );
                }
            });
        },
        init() {
            this.initOptions();
            this.queryTrainingLevel(this.queryForm.projId);
            getPower().then((res) => {
                this.role = res.data.power
                if(res.data.departmentCode.indexOf(",") !=-1){
                    let departmentCode = res.data.departmentCode.split(",")
                    this.queryForm.departmentId = departmentCode[0]
                    this.departmentId = departmentCode[0]
                }else{
                    this.queryForm.departmentId = res.data.departmentCode
                    this.departmentId = res.data.departmentCode
                }
                if(res.data.departmentCode && res.data.power == 3){
                    this.queryCampusMajor()
                }
            }).finally(()=>{
                this.filterSubmit();
            })
        },
        handleSearch() {
            const that = this;
            that.loading = true;
            API.findStudentCultureRelList(that.queryForm)
                .then(
                    function(res) {
                        that.loading = false;
                        if (res && res.data) {
                            that.tableData = res.data.list;
                            that.queryForm.total = res.data.total_;
                        }
                    },
                    function(err) {
                        that.loading = false;
                        that.$message.error({
                            showClose: true,
                            message: err.toString(),
                            duration: 2000
                        });
                    }
                )
                .catch(function(error) {
                    that.loading = false;
                    that.$message.error({
                        showClose: true,
                        message: "请求出现异常",
                        duration: 2000
                    });
                });
        },
        handleSizeChange(val) {
            this.queryForm.pageSize_ = val;
            this.handleSearch();
        },
        handleCurrentChange(val) {
            this.queryForm.pageNum_ = val;
            this.handleSearch();
        },
        filterSubmit() {
            this.queryForm.pageNum_ = 1;
            this.handleSearch();
        },
        reset(ref) {
            if(this.role == 3){
                this.$refs[ref].resetFields();
                this.init()
            }else{
                this.$refs[ref].resetFields();
                this.options.campusProfessions = [];
                this.queryForm.teacherName = "";
                this.options["major"] = [];
                this.filterSubmit();
            }
            
        },
        addStudentCultureRelByManual(row) {
            this.currentSelected = row;
            this.dialogVisible = true;
        },
        getSchemeId(schemeId) {
            this.schemeId = schemeId;
        },
        handleRouter(){
            let id =  this.newId;
            this.dialogOk =false;
            this.$router.push({
                name: "publicExport",
                params: {
                    id
                }
            });
            this.handleSearch();
        },
        handleSubmit() {
            let params = {};
            const self = this;

            if (!_.isArray(this.currentSelected)) {
                params = {
                    studentId: this.currentSelected.studentId,
                    cultureId: this.schemeId,
                    projId: this.queryForm.projId
                };
                API.addStudentCultureRelByManual(params).then(res => {
                    if (res && res.code === 200) {
                        self.$message({
                            message: "关联成功！",
                            type: "success"
                        });
                        this.dialogVisible = false;
                        self.handleSearch();
                    }
                });
            } else {
                params = {
                    idList: this.currentSelected,
                    cultureId: this.schemeId
                };
                API.batchAddStudentCultureRelByManual(params).then(res => {
                    if (res && res.code === 200) {
                        // self.$notify.success({
                        //     title: "成功",
                        //     message: "关联成功！"
                        // });
                        self.$message({
                            message: "关联成功！",
                            type: "success"
                        });
                        this.dialogVisible = false;
                        self.handleSearch();
                    }
                });
            }
        },
        cancelStudentCultureRelByManual(row) {
            const self = this;
            const data = {
                id: row.id,
                //cultureId: row.cultureId
            };
            this.$confirm("此操作将解除绑定关系, 是否继续?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
            })
            .then(() => {
                API.cancelStudentCultureRelByManual(data).then(res => {
                    if (res && res.code === 200) {
                        // self.$notify.success({
                        //     title: "成功",
                        //     message: "取消关联成功！"
                        // });
                        self.$message({
                            message: "取消关联成功！",
                            type: "success"
                        });
                    self.handleSearch();
                }
                });
            })
            .catch(e => {});
        },
        batchCancelRel() {
            const self = this;
            const params = {
                idList: []
            };

            if (this.$refs.studentTable.selection.length === 0) {
                this.$message({
                    message: "您还没有选择学生!",
                    type: "warning"
                });
                return;
            }
            if (this.$refs.studentTable.selection.length > 0) {
                this.$confirm("此操作将解除绑定关系, 是否继续?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(() => {
                    let idList = _.map(
                        this.$refs.studentTable.selection,
                        item => {
                            return _.pick(item, ["id"]);
                        }
                    );
                    this.doUnbindRel({ idList: idList }, this);
                });
            }
        },
        doUnbindRel(params, that) {
            API.batchCancelStudentCultureRelByManual(params).then(res => {
                if (res && res.code === 200) {
                    // that.$notify.success({
                    //     title: "成功",
                    //     message: "取消关联成功！"
                    // });
                    that.$message({
                        message: "取消关联成功！",
                        type: "success"
                    });
                    that.popoverVisible = false;
                    that.handleSearch();
                }
            });
        },
        syncStudentData() {
            API.studentInfSchronize().then(res => {
                if (res && res.code === 200) {
                    this.$message({
                        type: "success",
                        message: "同步数据成功！"
                    });
                } else {
                    this.$message({
                        type: "error",
                        message: "同步数据失败！"
                    });
                }
            });
        },
        batchAddRel() {
            const params = [];
            if (this.$refs.studentTable.selection.length === 0) {
                this.$message({
                    type: "warning",
                    message: "您还没有选择学生！"
                });
                return;
            }
            _.forEach(this.$refs.studentTable.selection, item => {
                params.push(_.pick(item, ["studentId"]));
            });
            this.currentSelected = params;
            this.dialogVisible = true;
        },
        async autoatchAddRel() {
            // this.queryForm.associationStatus = "1";
            const _this = this;
            let studentIds=[];//保存选中的学生
            let trainingLevelCode='';
            let obj={};
             _.forEach(this.$refs.studentTable.selection, item => {
               studentIds.push(item.studentId);
               trainingLevelCode=item.trainingLevelCode;
            });
            if(this.$refs.studentTable.selection.length == 0){
                obj.trainingLevel=this.queryForm.trainingLevel;
            }else{
                obj.trainingLevel=trainingLevelCode;
            }
            obj.studentIds=studentIds;
            obj.trainingCategory = _this.queryForm.trainingCategory.toString();
            let queryForm=Object.assign({}, _this.queryForm, obj);//保证原数据不变
            const numberRes = await API.countNeedBindSchemeStudent(queryForm);
            if (numberRes && numberRes.code === 200) {
                _this
                    .$confirm(
                        "本次关联培养方案" + numberRes.data + "个，是否关联？",
                        "自动关联",
                        {
                            confirmButtonText: "确定",
                            cancelButtonText: "取消",
                            type: "warning"
                        }
                    )
                    .then(() => {
                        API.bindSchemeStudent(
                            _.omit(queryForm, [
                                "pageSize_",
                                "pageNum_",
                                "total"
                            ])
                        ).then(res => {
                            let {
                                code,
                                data: { id },
                                data
                            } = res;
                            if (code === 200) {
                                this.tipsData = res.data
                                this.dialogOk = true;
                                this.newId = id
                            }
                        })
                        .catch(err => {
                            //  this.$confirm(
                            //     "关联失败",
                            //     "自动关联",
                            //     {
                            //         confirmButtonText: "确定",
                            //         cancelButtonText: "取消",
                            //         type: "warning"
                            //     }
                            // )
                    });
                    })
                    // .catch(err => {
                    //     this.$confirm(
                    //     "本次关联培养方案" + numberRes.data + "个，是否关联？",
                    //     "自动关联",
                    //     {
                    //         confirmButtonText: "确定",
                    //         cancelButtonText: "取消",
                    //         type: "warning"
                    //     }
                    // )
                    // });
                // _this.currentSelected = params;
                // _this.dialogVisible = true;
            }
        },
        handSearchChang() {
            this.filterSubmit();
        },
        //下载
        downloadfile(){
            downloadfile(this.tipsData.objectkey)
        },
        initOptions() {
            findFaculty().then(res => { 
                let data = {"X_YX": res.data};
                UTILS.fillSelect({ faculty: "X_YX" }, data, this.options);
            });
            const data = {
                lang: this.$store.getters.language || "cn",
                type: "allChild",
                keys: [
                    "X_PYCC",
                    "K_XXXS",
                    "X_ZXJH",
                    "X_PYLB",
                    "X_XWLB",
                    "X_XWLX",
                    "X_GLBM",
                    "X_RXJJ",
                    "X_XQ",
                    "X_LXZT",
                    "isAborad"
                ]
            };
            queryDic(data)
                .then(res => {
                    let params = {
                    lang: this.$store.getters.language,
                    keys: ["X_PYLB"]
                };
                sortTrainingCategories(params).then((res) => {
                    this.options.trainingCategory = res.data.X_PYLB;
                });
                    if (res.code === 200) {
                        // this.fillSelect(res.data);
                        UTILS.fillSelect(
                            {
                                //trainingLevel: "X_PYCC",
                                studentCategory: "X_XWLB",
                                spcialPlan: "X_ZXJH",
                                formLearning: "K_XXXS",
                                degreeType: "X_XWLX",
                                // trainingCategory: "X_PYLB",
                                project: "X_GLBM",
                                enrolSeason: "X_RXJJ",
                                campus: "X_XQ",
                                leaveSchool: "X_LXZT",
                                isOverseas: "isAborad"
                            },
                            res.data,
                            this.options
                        );
                        // this.onSubmit();
                    }
                })
                .catch(error => {
                    console.log(error);
                    // this.onSubmit();
                });
        },
        changeFaculty(val) {
            this.options["major"] = [];
            const data = {
                lang: this.$store.getters.language || "cn",
                type: "allChild",
                keys: ["G_ZY"],
                filter: {
                    prefix: [val],
                    specilaCode: "XY2ZY"
                }
            };
            queryDic(data)
                .then(res => {
                    if (res.code === 200) this._fill("major", res.data.G_ZY);
                })
                .catch(error => {
                    console.log(error);
                });
        },
        _fill(type, data) {
            for (const key in data) {
                this.options[type].push({
                    value: key,
                    label: data[key]
                });
            }
        },
        queryMajors() {
            // if (this.queryForm.departmentId) {
                this.options["major"] = [];
                const data = {
                    college: this.queryForm.departmentId,
                    type:this.queryForm.degreeType,
                    page: false
                };
                getGraduateDegreePage(data).then(res => {
                    if (res.code === 200) {
                        this.options["major"] = [];
                        let majors = _.map(res.data.list, item => {
                            return {
                                value: item.code,
                                label:
                                    "cn" !== this.$store.getters.language
                                        ? '(' + item.code + ')' + item.nameEn
                                        : '(' + item.code + ')' + item.name
                            };
                        });
                        majors &&
                            0 < majors.length &&
                            (this.options["major"] = [...majors]);
                    }
                });
            // }
        },
        queryTrainingLevel(val) {
            findTrainingLevelByProjId(val).then(res => {
                let { code, data } = res;

                if (code === 200) {
                    this.queryForm.trainingLevel = "";
                    this.options["trainingLevel"] = [];
                    _.each(data, level => {
                        this.options["trainingLevel"].push({
                            label:
                                "en" === this.$store.getters.language
                                    ? level.nameEN
                                    : level.nameCN,
                            value: level.code
                        });
                    });
                }
            });
        },
        pageExport() {
            let ids = this.$refs.studentTable.selection.map(item => item.studentId);
            let params = _.cloneDeep(this.queryForm);
            exportPage(
                Object.assign(params, {
                    ids: ids + ""
                }),'exportCultureRelList',"培养方案关联"
            );
        }
    }
};
</script>

<style scoped>
    /*ie11 css hack*/ 
    @media all and (-ms-high-contrast:none) { 
        *::-ms-backdrop,.ie-hack{ height: 51.89px !important; }
    } 
    .div-main {
        margin-bottom: 16px;
    }
    .waining-color i{
       color:#1a68df;
       font-size: 24px;
    }
    .waining-color span{
        margin-left: 10px;
       font-size: 20px;
       font-weight: bold;
    }
</style>
